package com.spleeb.pixelengine;


public class Model implements ModelData {
	public int[] colors;
	public int[] inds;
	public int width;
	public int height;
	private int wsub;
	private int wadd;
	private int hsub;
	private int hadd;
	public Model(int width,int height,int[] inds,int[] colors){
		this.colors = colors;
		this.inds = inds;
		this.width = width;
		this.height = height;
		fixDimensions();
	}
	public Model(){
		this.colors = new int[0];
		this.inds = new int[0];
		this.width = 0;
		this.height = 0;
		fixDimensions();
	}
	public void fixDimensions(){
		wsub = width/2;
		wadd = width - wsub;
		hsub = height/2;
		hadd = height - hsub;
	}
	public Model scheme(int[] ncolors){
		return new Model(width,height,inds,ncolors);
	}
	public void ColorToAlpha(int color){
		for (int i = 0 ;i<colors.length;i++){
			if (colors[i] == color){
				colors[i] = 1;
			}
		}
	}
	public void ScaleUp(){
		int[] nind = new int[width * height * 4];
		for (int i = 0;i<width;i++){
			for (int u = 0;u<height;u++){
				int ind = inds[i*height + u];
				nind[i*height*4 + u*2] = ind;
				nind[(i*2+1)*height*2 + u*2] = ind;
				nind[(i*2+1)*height*2 + u*2 + 1] = ind;
				nind[i*height*4 + u*2 + 1] = ind;
			}
		}
		width *= 2;
		height *= 2;
		fixDimensions();
		inds = nind;
	}
	public void Draw(Screen screen, Satrix satrix) {
		if (satrix.rot==0){
			primDraw(screen,satrix);
			return ;
		}else{
			int[] can = new int[width*height];
			final float xs = GameMath.cos(satrix.rot);
			final float ys = GameMath.sin(satrix.rot);
			final float off = (xs * width + ys * height)/2;
			final float x = -1* off;
			final float y = -1* off;
			for (int i = 0;i<width;i++){
				final float ixs_x = x + (i*xs);
				for (int u = 1;u<height;u++){
					int fx = (int) (ixs_x + (ys*(height-u)) + width/2);
					int fy = (int) (y + (u*xs) + (i*ys) + height/2);
					if (fx>=0&&fx<width&&fy>=0&&fy<height){
					can[fx*height +fy] = inds[i*height+u];
					}
				}
			}
			for (int i = 1;i<width -1;i++){
				int above = 0;
				for (int u = 1;u<height -1;u++){
					int curr = can[i*height + u];
					if (above!=0&&curr==0){
						if (can[(i-1)*height + u] != 0 && can[(i+1)*height + u] != 0){
							can[i*height + u] = above;
						}
						above = 0;
					}else{
						above = curr;
					}
				}
			}
			primDraw(screen,satrix,can);
		/*final float xs = GameMath.cos(satrix.rot);
		final float ys = GameMath.sin(satrix.rot);
		final float off = (xs * width + ys * height)/2;
		final float x = satrix.x - off;
		final float y = satrix.y - off;
		for (int i = 0;i<width;i++){
			final float ixs_x = x + (i*xs);
			for (int u = 0;u<height;u++){
				screen.set(ixs_x + (ys*(height-u)), y + (u*xs) + (i*ys), colors[inds[i*height+u]]);
			}
		}*/
		}
	}
	public void primDraw(Screen screen,Satrix satrix){
		primDraw(screen,satrix,inds);
	}
	public void primDraw(Screen screen,Satrix satrix,int[] arr){
		final int dx = (int)(satrix.x - wsub);
		final int dy = (int)(satrix.y - hsub);
		final int x = screen.chbx(dx);
		final int y = screen.chby(dy);
		final int w = screen.chbx((int)satrix.x + wadd);
		final int h = screen.chby((int)satrix.y + hadd);
		final int inc = y-dy;
		int done = (x-dx);
		for (int i = x;i<w;i++){
			int no = done * height + inc;
				for (int u = y;u<h;u++){
				screen.setpc(i, u, colors[arr[no]]);
				no++;
			}
				done ++;
		}
	}

}
